<?php
class hschool_apply_org{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
	public static $all_apply_org_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}

	/*
	*	取出一个信息
	*/
	public function get_one($id='') {
		$info = array();
		if($id){
			$sql = "SELECT * FROM ".DB::table('apply_org')." WHERE id='".$id."' LIMIT 1";
			$info = DB::fetch_first($sql);
            $sql = "SELECT d_name,d_parent_id,d_order FROM ".DB::table('dictionary')." WHERE d_id='".$info['d_id']."' LIMIT 1";
			$d_info = DB::fetch_first($sql);
            $info['d_name'] = $d_info['d_name'];
            $info['d_order'] = $d_info['d_order'];
            $info['d_parent_id'] = $d_info['d_parent_id'];
			if($d_info['d_parent_id']){
				$info['d_parent_name'] = ext::d_name($d_info['d_parent_id']);
			}
		}
		return $info;
	}

	/*
	*	创建一个信息
	*/
	public function add() {
		global $_G;
	}

	/*
	*	编辑审核
	*/
	public function edit() {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){
			$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
			$flag = isset($_REQUEST['flag']) ? $_REQUEST['flag']:'';
			if($id){
				$effect1 = DB::update('apply_org',array('flag'=>$flag),array('id'=>$id));
			}
			if($effect1){
				//DEBUG 插入机构关系
				if(1==$flag){
					$info = $this->get_one($id);
					$sql = "SELECT user_role_id FROM ".DB::table('users')." WHERE user_id = '".$info['user_id']."' LIMIT 1";
					$result_user_role_id = DB::result_first($sql);
					$data = array(
						'ou_d_id'=>$info['d_id'],
						'ou_user_id'=>$info['user_id'],
						'user_role_id'=>$result_user_role_id
					);
					//DEBUG 判断同网校用户是否已存在 开始
					$sql = "SELECT ou_id FROM ".DB::table('org_user')." WHERE ou_d_id = '".$info['d_id']."' AND ou_user_id = '".$info['user_id']."' LIMIT 1";
					$result_ou_id = DB::result_first($sql);
					//DEBUG 判断同网校用户是否已存在 结束
					if(empty($result_ou_id)){
						@DB::insert('org_user',$data);
					}
				}
				self::return_json(false,'操作成功');
			}else{
				self::return_json();
			}
		}else{
			$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
			$info = $this->get_one($id);
			include template('admin/hschool/apply_org_edit');
		}
	}

	/*
	*	删除课时
	*/
	public function del() {
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){
			$d_ids_array = array();
			$d_ids_array = isset($_REQUEST['d_ids']) ? $_REQUEST['d_ids']:array();
			if($d_ids_array){
				$where = ' id IN ('.dimplode($d_ids_array).') ';
				$effects1 = DB::delete('apply_org',$where);
			}
			//DEBUG 接收用户信息 end
			$this->return_json(false,'操作成功');
			echo json_encode($return_array);
		}
	}

	/*
	*	当前动作的首页
	*/
	public function index() {
		global $_G;
		$page_array = array();
		$sqlcount = $sql = $wheresql = $keyword = $pageNum = $numPerPage = '';
		$pageNum = isset($_REQUEST['pageNum']) ? $_REQUEST['pageNum'] : 1;
		$numPerPage = isset($_REQUEST['numPerPage']) ? $_REQUEST['numPerPage'] : 10;
		$pagestart = ($pageNum - 1) * $numPerPage;
		//DEBUG 获取用户所在网校
		$wheresql = '';
		if(88 != $_G['user_id'] && 1 != $_G['user_id']){
			$sql = "SELECT ou_d_id FROM ".DB::table('org_user')." WHERE ou_user_id = '".$_G['user_id']."' LIMIT 1";
			$result_ou_d_id = DB::result_first($sql);
			$wheresql = " WHERE s.d_id = '".$result_ou_d_id."' ";
		}
		$sqlcount = "SELECT count(*) from ".DB::table('apply_org')." AS s ".$wheresql;
		$pagetotal = DB::result_first($sqlcount);
		$sql = "SELECT s.* from ".DB::table('apply_org')." AS s ".$wheresql." ORDER BY s.id DESC LIMIT ".$pagestart.",".$numPerPage;
		$page_array = DB::fetch_all($sql);
		foreach($page_array AS $key => $value){
			$page_array[$key]['d_name'] = ext::d_name($value['d_id']);
			$page_array[$key]['user_realname'] = ext::user_realname($value['user_id']);
		}
		//总页数
		$realpages = @ceil($pagetotal / $numPerPage);
		if($realpages <= 0){
			$realpages=1;
		}
		//前一页
		$page_pre = max(1, intval(($pageNum-1)));
		//后一页
		$page_next = min(intval(($pageNum+1)), $realpages);
		include template('admin/hschool/apply_org');
	}
    
	/*
	* ajax 返回信息
	*/
	public function return_json($error='true',$message="操作失败") {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		echo json_ext($return_array);
		die;
	}
}
?>